//
//  TransitionAnimation.m
//  BaseCode
//
//  Created by 曹连华 on 2019/7/3.
//  Copyright © 2019 曹连华. All rights reserved.
//

#import "TransitionAnimation.h"

@implementation TransitionAnimation
#pragma mark - UIViewControllerAnimatedTransitioning

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
    return [self transitionAnimationDuration];
}



- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    switch (self.transitionType) {
        case TransitionTypePush:
        case TransitionTypePop:
            [self push_pop_animateTransition:transitionContext];
            break;
        case TransitionTypePresent:
        case TransitionTypeDismissed:
            [self present_dismissed_animateTransition:transitionContext];
            break;
        default:
            break;
    }

}

- (void)push_pop_animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toViewController.view];
    [containerView addSubview:fromViewController.view];
    CGRect finalFrame = [transitionContext finalFrameForViewController:toViewController];
    
    
    float deviation = (self.transitionType == TransitionTypePush) ? 1.0f : -1.0f;
    CGRect newFrame = finalFrame;
    newFrame.origin.x += newFrame.size.width * deviation;
    toViewController.view.frame = newFrame;
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        toViewController.view.frame = finalFrame;
        
        CGRect animationFrame = fromViewController.view.frame;
        animationFrame.origin.x -= animationFrame.size.width * deviation;
        fromViewController.view.frame = animationFrame;
        
    }completion:^(BOOL finished) {
        [toViewController.tabBarController.view setNeedsLayout];//tbc的bug，所以得重新刷新界面，不然tabbar会有问题
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

- (void)present_dismissed_animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:fromViewController.view];
    [containerView addSubview:toViewController.view];
    float deviation = (self.transitionType == TransitionTypePresent) ? 1.0f : -1.0f;
    
    CGRect finalFrame = [transitionContext finalFrameForViewController:toViewController];
    CGRect newFrame = [transitionContext finalFrameForViewController:toViewController];
    newFrame.origin.y += newFrame.size.height * deviation;
    
    
    if(self.transitionType == TransitionTypePresent) {
        toViewController.view.frame = newFrame;
    } else if (self.transitionType == TransitionTypeDismissed) {
        toViewController.view.frame = finalFrame;
        [containerView bringSubviewToFront:fromViewController.view];
    }

    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        if (self.transitionType == TransitionTypePresent) {
            toViewController.view.frame = finalFrame;
        } else if (self.transitionType == TransitionTypeDismissed) {
            CGRect animationFrame = fromViewController.view.frame;
            animationFrame.origin.y -= animationFrame.size.height * deviation;
            fromViewController.view.frame = animationFrame;
        }
        
    }completion:^(BOOL finished) {
        [toViewController.tabBarController.view setNeedsLayout];//tbc的bug，所以得重新刷新界面，不然tabbar会有问题
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

- (NSTimeInterval)transitionAnimationDuration {
    return 0.34;
}

- (CABasicAnimation *)transitionBasicAnimationPath:(NSString *)keyPath {
    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:keyPath];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.duration = [self transitionAnimationDuration];
    animation.repeatCount = 1;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    return animation;
}

- (CAKeyframeAnimation *)transitionKeyframeAnimationPath:(NSString *)keyPath {
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:keyPath];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.duration = [self transitionAnimationDuration];
    animation.repeatCount = 1;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    return animation;
}

- (CASpringAnimation *)transitionSpringAnimationPath:(NSString *)keyPath  API_AVAILABLE(ios(9.0)){
    if (@available(iOS 9.0, *)) {
        CASpringAnimation* animation = [CASpringAnimation animationWithKeyPath:keyPath];
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        animation.duration = [self transitionAnimationDuration];
        animation.repeatCount = 1;
        animation.fillMode = kCAFillModeForwards;
        animation.removedOnCompletion = NO;
        return animation;
    } else {
        // Fallback on earlier versions
        return nil;
    }
}

- (CAAnimationGroup *)transitionGroupAnimation {
    CAAnimationGroup* animation = [CAAnimationGroup animation];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.duration = [self transitionAnimationDuration];
    animation.repeatCount = 1;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    return animation;
}

- (void)dealloc {
    NSLog(@"TransitionAnimation - dealloc ");
}
@end
